function fn(num) {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve(num * 2)
        }, 1000)
    })
}

function* gen() {
    // Generator 函数
    const num1 = yield fn(1)
    const num2 = yield fn(num1)
    const num3 = yield fn(num2)
    return num3
}

function generatorToAsync(generatorFn) {
    return () => new Promise((resolve) => {
        const g = generatorFn()
        console.log(g)
        const next1 = g.next()
        console.log(next1)
        next1.value.then(res1 => {
            const next2 = g.next(res1)
            console.log(next2)
            next2.value.then(res2 => {
                const next3 = g.next(res2)
                console.log(next3)
                next3.value.then(res3 => {
                    const next4 = g.next(res3)
                    console.log(next4)
                    resolve(next4.value)
                })
            })
        })
    })
}

const asyncFn = generatorToAsync(gen)

console.log(asyncFn())
